package springinpractice.ch1;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import springinpractice.ch1.model.Account;
import springinpractice.ch1.service.AccountService;
import springinpractice.ch1.dao.AccountDao;

/*
 * @author	Willie Wheeler, John Wheeler and Zeeshan Maqbool
 * Book		'Spring In Practice' [Book written by Willie Wheeler and John Wheeler]
 */

public class ConsoleApp
{

	final static Logger log = Logger.getLogger(ConsoleApp.class);
	static ApplicationContext appCtx = null;

	public static void main(String[] args) throws Exception
	{
		appCtx = setContext();

		AccountService accountService = (AccountService) appCtx.getBean("accountService");
		List<Account> deliquentAccounts = accountService.findDelinquentAccounts();
		
		for (Account a : deliquentAccounts)
		{
			//System.out.println(a.getAccountNo());
			log.info(a.getAccountNo());
		}
	}

	private static ApplicationContext setContext()
	{
		/*
		 * Directory for context.xml
		 */
		String currentDir = System.getProperty("user.dir");
		String separator = System.getProperty("file.separator");
		String sourceDir = "resources" + separator;
		
		
		
		return new FileSystemXmlApplicationContext(separator + currentDir
				+ separator + sourceDir + "context.xml");

		/*
		 * Other ways to load the context file
		 */
		// Resource res1 =
		// ctx.getResource("file:///COMPLETE/PATH/TO/YOUR/FILE");
		// Resource res2 = ctx.getResource("classpath:com/yourName.class");

	}
}